from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from common.utils import Driver
from common.utils import ScreenShot


class BlogLogin:
    def __init__(self):
        Driver.get("http://8.137.19.140:9090/blog_login.html")

    @staticmethod
    def clean_input():
        Driver.find_element(By.CSS_SELECTOR, "#username").clear()
        Driver.find_element(By.CSS_SELECTOR, "#password").clear()

    # 1.登录成功
    def login_succeed(self):
        self.clean_input()
        Driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        Driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        Driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 找到头像说明登录成功
        WebDriverWait(Driver, 1).until(expected_conditions.presence_of_element_located(
            (By.CSS_SELECTOR, "body > div.container > div.left > div > img")))
        Driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > img")
        # 屏幕截图
        ScreenShot()

    # 2.异常登录
    # 不输入用户名和密码直接登录
    def fail_no_input(self):
        self.clean_input()
        Driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 切换到警告弹窗并确认
        WebDriverWait(Driver, 1).until(expected_conditions.alert_is_present())
        Driver.switch_to.alert.accept()
        ScreenShot()

    # 只输入用户名，不输入密码登录
    def fail_no_password(self):
        self.clean_input()
        Driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        Driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 切换到警告弹窗并确认
        WebDriverWait(Driver, 1).until(expected_conditions.alert_is_present())
        Driver.switch_to.alert.accept()
        ScreenShot()

    # 只输入密码，不输入用户名登录
    def fail_no_username(self):
        self.clean_input()
        Driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        Driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 切换到警告弹窗并确认
        WebDriverWait(Driver, 1).until(expected_conditions.alert_is_present())
        Driver.switch_to.alert.accept()
        ScreenShot()

    # 输入错误的用户名和密码登录
    def fail_wrong_input(self):
        self.clean_input()
        Driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhansan")
        Driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456789")
        Driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 切换到警告弹窗并确认
        WebDriverWait(Driver, 1).until(expected_conditions.alert_is_present())
        Driver.switch_to.alert.accept()
        ScreenShot()

    # 输入错误的密码登录
    def fail_wrong_password(self):
        self.clean_input()
        Driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        Driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456789")
        Driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 切换到警告弹窗并确认
        WebDriverWait(Driver, 1).until(expected_conditions.alert_is_present())
        Driver.switch_to.alert.accept()
        ScreenShot()

    # 输入错误的用户名登录
    def fail_wrong_username(self):
        self.clean_input()
        Driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhansan")
        Driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        Driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 切换到警告弹窗并确认
        WebDriverWait(Driver, 1).until(expected_conditions.alert_is_present())
        Driver.switch_to.alert.accept()
        ScreenShot()

    # 未注册的用户登录
    def fail_nonexistent_user(self):
        self.clean_input()
        Driver.find_element(By.CSS_SELECTOR, "#username").send_keys("admin123")
        Driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        Driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 切换到警告弹窗并确认
        WebDriverWait(Driver, 1).until(expected_conditions.alert_is_present())
        Driver.switch_to.alert.accept()
        ScreenShot()

    def test_case(self):
        self.login_succeed()
        # 返回登录页
        Driver.back()
        self.fail_no_input()
        self.fail_no_password()
        self.fail_no_username()
        self.fail_wrong_input()
        self.fail_wrong_password()
        self.fail_wrong_username()
        self.fail_nonexistent_user()
